<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 当冲产品
 */
namespace app\product\controller;

use  Mscore\Core\Controller;
use  app\product\model\Dueltrade as DueltradeModel;
use  app\product\model\Common as CommonModel;
use  Mscore\Core\Lib\ArrayMethod;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lang;

class DueltradeController extends Controller{

    private $model;
    private $login;
    private $error;
    private $success;
    private $language;
    
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->model = new DueltradeModel;
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    /**
     * 内容列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list(){  
    //文章类
        $post = post();
        switch (true) {
            case $post:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' a.dels<1 ';
                if(post('title')){
                    $where .= ' and a.title like "%'.post('title').'%"';
                }
                if(post('procode')){
                    $where .= ' and a.procode like "%'.post('procode').'%"';
                }
                if(post('cid')){
                    $where .= ' and a.cid ='.post('cid');
                }
                $datalist = $this->model->list($where,$page,$limit);
                $data = [
                    "code" => 200,
                    "data" => $datalist['datalist'],
                    "count"=> $datalist['total'],
                    "msg" => 'ok'
                ];
                $this->echojson($data);
                break;
            default:
                $cateclass = (new CommonModel())->getProCate('dels < 1');
                $this->assign('cateclass', ArrayMethod::recurse($cateclass));
                $this->display('list.html');
                break;
        }
    }

    /**
     * 内容添加
     * @param array $add 
     * @param array $where 
     */ 
    public function add(){
        $params = post();
        switch (true) {
            case $params:
                if($params['beatpoint']['low']>$params['beatpoint']['top']){
                    $this->error['msg']='参数非法，最小倍数不能大于最大倍数';
                    logrecord('添加/修改当冲产品:'.$this->error['msg'],$this->login['info']);
                    $this->echoJson($this->error);
                }
                $data = $this->model->save($params);
                switch ($data) {
                    case true:
                        if($params['status']>2){
                            $key = config('futures.dueltrade');
                            $this->model->execRedis('hdel',[$key,$params['procode']]); 
                        }
                        logrecord('添加/修改当冲产品:成功',$this->login['info']);
                        $this->echoJson($this->success);
                        break;
                    default:
                        logrecord('添加/修改当冲产品:失败',$this->login['info']);
                        $this->echoJson($this->error);
                        break;
                }  
                break;
            default:
                if(get('id')){
                    $datainfo =$this->model->info(get('id'));
                }
                $class = ArrayMethod::recurse((new CommonModel)->getProCate('','id,title,pid'));
                //定义变量datainfo输出
                $datainfo['id']=isset($datainfo['id'])?$datainfo['id']:'';
                $datainfo['title']=isset($datainfo['title'])?$datainfo['title']:'';
                $datainfo['cid']=isset($datainfo['cid'])?$datainfo['cid']:0;
                $datainfo['cname']=isset($datainfo['cname'])?$datainfo['cname']:'';
                $datainfo['procode']=isset($datainfo['procode'])?$datainfo['procode']:'';
                $datainfo['proncode']=isset($datainfo['proncode'])?$datainfo['proncode']:'';
                $datainfo['langinfo']=isset($datainfo['langinfo'])?$datainfo['langinfo']:array();
                $datainfo['opentype']=isset($datainfo['opentype'])?$datainfo['opentype']:1;
                $datainfo['opentime'] = !empty($datainfo['opentime']) ? $datainfo['opentime'] : array(0 => '', 1 => '', 2 => '', 3 => '', 4 => '', 5 => '', 6 => '');
                $datainfo['beatpoint']=isset($datainfo['beatpoint'])?$datainfo['beatpoint']: array('low'=>0,'top'=>0,'rands'=>0,'length'=>2);
                $datainfo['orderid']=isset($datainfo['orderid'])?$datainfo['orderid']:'';
                $datainfo['status']=isset($datainfo['status'])?$datainfo['status']:3;
                $datainfo['imgurl']=!empty($datainfo['imgurl'])?$datainfo['imgurl']:'';
                $datainfo['online']=!empty($datainfo['online'])?$datainfo['online']:"100-100";

                $langs=(new CommonModel)->getLang();
                $this->assign('langs',$langs);
                $this->assign('datainfo',$datainfo);
                $this->assign('class',$class);
                $this->display('add.html');
                break;
        }
    }

    /**
     * 内容删除
     * @param array $del 
     * @param array $where 
     */
    public function del(){
        $data=$this->model->del(post('id',false));
        switch ($data) {
            case true:
                logrecord('删除当冲产品:成功',$this->login['info']);
                $this->echoJson($this->success);
                break;
                    
            default:
                logrecord('删除当冲产品:失败',$this->login['info']);
                $this->echoJson($this->error);
                break;
        }  
    }   
}
